package org.example.java8.future;

import java.util.concurrent.CompletableFuture;

//36CompletableFuture常用API的重点详解-上
public class CompletableFutureInAction7 {
    public static void main(String[] args) throws InterruptedException {
        CompletableFuture.supplyAsync(() -> 1)
                .thenApply(integer -> Integer.sum(integer, 10))
                .whenComplete((v, t) -> System.out.println(v));
        //.whenCompleteAsync((v,t)-> System.out.println(v));


        CompletableFuture.supplyAsync(() -> 1)
                .handle((v, t) -> Integer.sum(v, 10))
                .whenComplete((v, t) -> System.out.println(v))
                .thenRun(System.out::println);

        CompletableFuture.supplyAsync(() -> 1)
                .thenApply(integer -> Integer.sum(integer, 10))
                .thenAccept(System.out::println);

        CompletableFuture.supplyAsync(() -> 1)
                .thenCompose(i -> CompletableFuture.supplyAsync(() -> 10 * i))
                .thenAccept(System.out::println);

        CompletableFuture.supplyAsync(() -> 1)
                .thenCombine(CompletableFuture.supplyAsync(() -> 2.0d), (r1, r2) -> r1 + r2)
                .thenAccept(System.out::println);

        CompletableFuture.supplyAsync(() -> 1)
                .thenAcceptBoth(CompletableFuture.supplyAsync(() -> 2.0d), (r1, r2) -> {
                    System.out.println(r1);
                    System.out.println(r1 + r2);
                });


        Thread.sleep(1000L);

    }
}
